﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using Microsoft.Practices.Composite.Presentation.Commands;

namespace NetManager.Core.UI.Commands
{
    public class MouseDoubleClickCommandBehavior : CommandBehaviorBase<Control>
    {
        public MouseDoubleClickCommandBehavior(Control targetObject)
            : base(targetObject)
        {
            targetObject.MouseDoubleClick += (s, e) => base.ExecuteCommand();
        }
    }

    public static class MouseDoubleClick
    {
        public static readonly DependencyProperty MouseDoubleClickBehaviorProperty =
            DependencyProperty.RegisterAttached(
                "MouseDoubleClickBehaviorProperty", typeof(MouseDoubleClickCommandBehavior),
                typeof(MouseDoubleClickCommandBehavior), null);

        #region CommandProperty

        public static readonly DependencyProperty CommandProperty =
            DependencyProperty.RegisterAttached(
                "Command", typeof(ICommand), typeof(MouseDoubleClick),
                new PropertyMetadata(CommandProperty_Changed));

        public static ICommand GetCommand(DependencyObject obj)
        {
            return (ICommand)obj.GetValue(CommandProperty);
        }

        public static void SetCommand(DependencyObject obj, ICommand value)
        {
            obj.SetValue(CommandProperty, value);
        }

        private static void CommandProperty_Changed(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
        {
            Control targetObject = dependencyObject as Control;

            if (targetObject != null)
                GetOrCreateBehavior(targetObject).Command = e.NewValue as ICommand;
        }

        #endregion

        #region CommandParameterProperty

        public static readonly DependencyProperty CommandParameterProperty =
            DependencyProperty.RegisterAttached(
                "CommandParameter", typeof(object),
                typeof(MouseDoubleClick), new PropertyMetadata(CommandParameterProperty_Changed));

        public static ICommand GetCommandParameter(DependencyObject obj)
        {
            return (ICommand)obj.GetValue(CommandParameterProperty);
        }

        public static void SetCommandParameter(DependencyObject obj, ICommand value)
        {
            obj.SetValue(CommandParameterProperty, value);
        }

        private static void CommandParameterProperty_Changed(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
        {
            Control targetObject = dependencyObject as Control;

            if (targetObject != null)
                GetOrCreateBehavior(targetObject).CommandParameter = e.NewValue;
        }

        #endregion

        private static MouseDoubleClickCommandBehavior GetOrCreateBehavior(Control targetObject)
        {
            MouseDoubleClickCommandBehavior behavior = targetObject.GetValue(MouseDoubleClickBehaviorProperty) as MouseDoubleClickCommandBehavior;

            if (behavior == null)
            {
                behavior = new MouseDoubleClickCommandBehavior(targetObject);
                targetObject.SetValue(MouseDoubleClickBehaviorProperty, behavior);
            }

            return behavior;
        }
    }
}
